<template>
  <el-dialog
    :title="!dataForm.roleId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="角色名称" prop="roleName">
      <el-input v-model="dataForm.roleName" placeholder="角色名称"></el-input>
    </el-form-item>
    <el-form-item label="角色权限字符串" prop="roleKey">
      <el-input v-model="dataForm.roleKey" placeholder="角色权限字符串"></el-input>
    </el-form-item>
    <el-form-item label="显示顺序" prop="roleSort">
      <el-input v-model="dataForm.roleSort" placeholder="显示顺序"></el-input>
    </el-form-item>
    <el-form-item label="数据范围（1：全部数据权限 2：自定数据权限 3：本部门数据权限 4：本部门及以下数据权限）" prop="dataScope">
      <el-input v-model="dataForm.dataScope" placeholder="数据范围（1：全部数据权限 2：自定数据权限 3：本部门数据权限 4：本部门及以下数据权限）"></el-input>
    </el-form-item>
    <el-form-item label="菜单树选择项是否关联显示" prop="menuCheckStrictly">
      <el-input v-model="dataForm.menuCheckStrictly" placeholder="菜单树选择项是否关联显示"></el-input>
    </el-form-item>
    <el-form-item label="部门树选择项是否关联显示" prop="deptCheckStrictly">
      <el-input v-model="dataForm.deptCheckStrictly" placeholder="部门树选择项是否关联显示"></el-input>
    </el-form-item>
    <el-form-item label="角色状态（0正常 1停用）" prop="status">
      <el-input v-model="dataForm.status" placeholder="角色状态（0正常 1停用）"></el-input>
    </el-form-item>
    <el-form-item label="删除标志（0代表存在 2代表删除）" prop="delFlag">
      <el-input v-model="dataForm.delFlag" placeholder="删除标志（0代表存在 2代表删除）"></el-input>
    </el-form-item>
    <el-form-item label="创建者" prop="createBy">
      <el-input v-model="dataForm.createBy" placeholder="创建者"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新者" prop="updateBy">
      <el-input v-model="dataForm.updateBy" placeholder="更新者"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          roleId: 0,
          roleName: '',
          roleKey: '',
          roleSort: '',
          dataScope: '',
          menuCheckStrictly: '',
          deptCheckStrictly: '',
          status: '',
          delFlag: '',
          createBy: '',
          createTime: '',
          updateBy: '',
          updateTime: '',
          remark: ''
        },
        dataRule: {
          roleName: [
            { required: true, message: '角色名称不能为空', trigger: 'blur' }
          ],
          roleKey: [
            { required: true, message: '角色权限字符串不能为空', trigger: 'blur' }
          ],
          roleSort: [
            { required: true, message: '显示顺序不能为空', trigger: 'blur' }
          ],
          dataScope: [
            { required: true, message: '数据范围（1：全部数据权限 2：自定数据权限 3：本部门数据权限 4：本部门及以下数据权限）不能为空', trigger: 'blur' }
          ],
          menuCheckStrictly: [
            { required: true, message: '菜单树选择项是否关联显示不能为空', trigger: 'blur' }
          ],
          deptCheckStrictly: [
            { required: true, message: '部门树选择项是否关联显示不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '角色状态（0正常 1停用）不能为空', trigger: 'blur' }
          ],
          delFlag: [
            { required: true, message: '删除标志（0代表存在 2代表删除）不能为空', trigger: 'blur' }
          ],
          createBy: [
            { required: true, message: '创建者不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateBy: [
            { required: true, message: '更新者不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.roleId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.roleId) {
            this.$http({
              url: this.$http.adornUrl(`/ruoyiapisystem/sysrole/info/${this.dataForm.roleId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.roleName = data.sysRole.roleName
                this.dataForm.roleKey = data.sysRole.roleKey
                this.dataForm.roleSort = data.sysRole.roleSort
                this.dataForm.dataScope = data.sysRole.dataScope
                this.dataForm.menuCheckStrictly = data.sysRole.menuCheckStrictly
                this.dataForm.deptCheckStrictly = data.sysRole.deptCheckStrictly
                this.dataForm.status = data.sysRole.status
                this.dataForm.delFlag = data.sysRole.delFlag
                this.dataForm.createBy = data.sysRole.createBy
                this.dataForm.createTime = data.sysRole.createTime
                this.dataForm.updateBy = data.sysRole.updateBy
                this.dataForm.updateTime = data.sysRole.updateTime
                this.dataForm.remark = data.sysRole.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/ruoyiapisystem/sysrole/${!this.dataForm.roleId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'roleId': this.dataForm.roleId || undefined,
                'roleName': this.dataForm.roleName,
                'roleKey': this.dataForm.roleKey,
                'roleSort': this.dataForm.roleSort,
                'dataScope': this.dataForm.dataScope,
                'menuCheckStrictly': this.dataForm.menuCheckStrictly,
                'deptCheckStrictly': this.dataForm.deptCheckStrictly,
                'status': this.dataForm.status,
                'delFlag': this.dataForm.delFlag,
                'createBy': this.dataForm.createBy,
                'createTime': this.dataForm.createTime,
                'updateBy': this.dataForm.updateBy,
                'updateTime': this.dataForm.updateTime,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
